package eu.webhib.extgui.update;

import java.util.*;
import org.apache.log4j.Logger;
import org.primefaces.context.RequestContext;

/**
 * Controls the partial update of components by ajax means as provided by view framework (primeFaces).
 */
public class UpdateMngr extends ArrayList<String> {

   private static final Logger log = Logger.getLogger(UpdateMngr.class);

   /**
    * If only one component is to be updated
    * @param componentId Id string of the component to be updated
    */
   public UpdateMngr(String componentId) {
      assert (componentId != null);
      this.add(componentId);
   }

   /**
    * If two components are to be updated
    * @param componentId1 Id string of the component1 to be updated
    * @param componentId2 Id string of the component2 to be updated
    */
   public UpdateMngr(String componentId1, String componentId2) {
      assert (componentId1 != null);
      assert (componentId2 != null);
      this.add(componentId1);
      this.add(componentId2);
   }

   /**
    * Take a list of component Ids
    * @param componentList list of component Id string to be updated
    */
   public UpdateMngr(List<String> componentList) {
      this.addAll(componentList);
   }

   /**
    * Update all components as provided to constructor
    */
   public void updateAll() {
      logOut("updateAll: ", this);

      /** set the components to be updated, thus returned to the browser */
      RequestContext.getCurrentInstance().addPartialUpdateTargets(this);
   }

   protected void logOut(String text, List<String> list) {
      if (log.isDebugEnabled()) {
         String print = text;
         for (String component : list) {
            print += component + " ";
         }
         log.debug(print);
      }
   }
}
